import Vue from '@/main.js'

import { checkInvitationCode, getRegisterCode, registerDo, userUpdate } from '@/api/user'

const oldState = {
  // 邀请码
  invitation_code: '',
  // 错误提示消息
  errorMessage: '',
  // 注册手机号
  phone: '',
  // 手机号下方错误提示
  phoneErrorMessage: '',
  // 手机验证码
  code: '',
  // 姓名
  name: '',
  // 密码
  password: '',
  // 姓名下方错误提示
  nameErrorMessage: ''
}

const state = JSON.parse(JSON.stringify(oldState))

const mutations = {
  // 设置原始状态
  set_state (state) {
    for (const key in state) {
      state[key] = oldState[key]
    }
  },
  // 设置邀请码
  set_invitation_code (state, code) {
    state.invitation_code = code
  },
  // 设置错误提示消息
  set_errorMessage (state, msg) {
    state.errorMessage = msg
  },
  // 设置注册手机号
  set_phone (state, phone) {
    state.phone = phone
  },
  // 设置手机号下方错误提示
  set_phoneErrorMessage (state, msg) {
    state.phoneErrorMessage = msg
  },
  // 设置手机验证码
  set_code (state, code) {
    state.code = code
  },
  // 设置姓名
  set_name (state, name) {
    state.name = name
  },
  // 设置密码
  set_password (state, password) {
    state.password = password
  },
  // 设置名字下方错误提示
  set_nameErrorMessage (state, msg) {
    state.nameErrorMessage = msg
  }
}

const actions = {
  // 校验验证码是否可用
  checkInvitationCode ({ state, commit }) {
    if (state.invitation_code) {
      return new Promise((resolve, reject) => {
        checkInvitationCode(state.invitation_code).then(res => {
          resolve(res)
          if (res.data.code === 200) {

          } else {
            commit('set_errorMessage', res.data.message)
          }
        })
      })
    } else {
      commit('set_errorMessage', '请输入邀请码')
    }
  },
  // 获取注册手机验证码
  getRegisterPhoneCode ({ state, commit, rootState }, params) {
    return new Promise((resolve, reject) => {
      let code
      if (params && params.captcha) {
        code = params.captcha
      }
      getRegisterCode(state.phone, code).then(res => {
        if (res.data.code === 200) {
          Vue.tools.countDown(60)
        } else if (res.data.code === 502) {
          if (!rootState.common.showImgDialog) {
            commit('common/set_showImgDialog', true, { root: true })
            commit('common/set_fnName', 'register/getRegisterPhoneCode', { root: true })
            commit('common/set_fnParams', {}, { root: true })
            commit('common/set_imgCode', '', { root: true })
          } else {
          }
        } else {
          commit('set_phoneErrorMessage', res.data.message)
        }
        resolve(res)
      })
    })
  },
  // 注册
  register ({ state, commit }) {
    return new Promise((resolve, reject) => {
      registerDo(state.phone, state.code, state.invitation_code).then(res => {
        switch (res.data.code) {
          case 200: {
            Vue.tools.storage.setHfSession(res.data.data.token)
            Vue.tools.storage.setSignUpTemp()
            commit('user/set_hfSession', res.data.data.token, { root: true })
            break
          }
          case 500: {
            commit('set_phoneErrorMessage', res.data.message)
            break
          }
          case 501: {
            commit('set_errorMessage', res.data.message)
            break
          }
        }
        resolve(res)
      })
    })
  },
  // 更新用户信息
  userInfoUpdate ({ state, commit }) {
    if (!state.name.trim()) {
      commit('set_nameErrorMessage', '请输入姓名')
      return false
    }
    if (!state.password) {
      commit('set_errorMessage', '请输入密码')
      return false
    }
    return new Promise((resolve, reject) => {
      userUpdate({ name: state.name.trim(), phone: '', password: state.password.trim(), confirm_password: state.password.trim() }).then(res => {
        switch (res.data.code) {
          case 200: {
            break
          }
          case 412: {
            commit('set_errorMessage', res.data.message)
            break
          }
          default: {
            commit('set_errorMessage', res.data.message)
            break
          }
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
